package com.wtwd.campus.encryptnetty;


import com.wtwd.campus.utils.AESUtils;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

// MessageToByteEncoder<I> 中的泛型表示原数据类型
public class EncryptMessageEncoder extends MessageToByteEncoder<Object> {

    private final static Logger logger = LoggerFactory.getLogger(EncryptMessageEncoder.class);

    @Override
    public void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out)
            throws Exception {
        logger.info("channel id:{},未加密之前={}", ctx.channel().id(), msg.toString());
        logger.info("channel id:{},发送加密消息={}", ctx.channel().id(), AESUtils.encrypt(msg.toString()));
        out.writeBytes(AESUtils.encrypt(msg.toString()).getBytes("utf-8"));
    }

    public static void main(String[] args) throws Exception {
        System.out.println(AESUtils.decrypt("/HnBMlWhnPjZjPOKFof4QzerxZyKSUbL1bdOMRKuidvDbVZob+WGRU4XI/Rfx2uMLcxW4mQjwyZlCfqYfBNwgw=="));
        System.out.println(AESUtils.encrypt("*WT,866248053256309,GETPHONE,161312,4,021122,FDFFFFFF#"));
    }
}
